package com.exam.sacrifice.ui.mine;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.View;

import com.exam.commonbiz.base.BaseActivity;
import com.exam.commonbiz.base.BaseViewModel;
import com.exam.commonbiz.util.OnFastClickListener;
import com.exam.commonbiz.util.ToastUtil;
import com.exam.sacrifice.R;
import com.exam.sacrifice.databinding.ActivityAboutBinding;
import com.pgyersdk.update.PgyUpdateManager;
import com.pgyersdk.update.UpdateManagerListener;
import com.pgyersdk.update.javabean.AppBean;

public class AboutActivity extends BaseActivity<BaseViewModel, ActivityAboutBinding> {

    @Override
    public void initData() {

        binding.include.tvTitle.setText("关于");
        binding.include.ivback.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                finish();
            }
        });

        binding.layoutCheckUpdate.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                new PgyUpdateManager.Builder().setForced(false).setUserCanRetry(false).setDeleteHistroyApk(true).setUpdateManagerListener(new UpdateManagerListener() {
                    @Override
                    public void onNoUpdateAvailable() {
                        ToastUtil.getInstance().showToast("当前已是最新版本");
                    }

                    @Override
                    public void onUpdateAvailable(AppBean appBean) {
                        new PgyUpdateManager.Builder().setForced(false)              //设置是否强制更新
                                .setUserCanRetry(false)        //失败后是否提示重新下载
                                .setDeleteHistroyApk(true)     // 检查更新前是否删除本地历史 Apk
                                .register();
                    }

                    @Override
                    public void checkUpdateFailed(Exception e) {
                        ToastUtil.getInstance().showToast(e.getMessage());
                    }
                }).register();
            }
        });

        PackageManager packageManager = getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            binding.tvVersion.setText("版本号: v" + packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_about;
    }
}